package com.youyou.fusheng2.View.WelcomPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.youyou.fusheng2.FuShengMain;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.Shaper;
import com.youyou.fusheng2.Utils.WindowTouMing;

import java.util.ArrayList;
import java.util.List;

public class WelcomPage extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager  viewpage;
    private List<View> views = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new WindowTouMing(this).windoe();
        if (Shaper.Get(this)){
            startActivity(new Intent(this, threeTimePage.class));
            this.finish();
        }else {
            setContentView(R.layout.welcom_page);
            initView();
        }
    }

    private void initView() {
        viewpage = (ViewPager) findViewById(R.id.viewpage);
        viewpage.setOnPageChangeListener(this);

        for (int i = 0; i <= 3; i++) {
            LayoutInflater from = LayoutInflater.from(this);
            View inflate = from.inflate(R.layout.viewpage_item, null);
            views.add(inflate);
            TextView image = inflate.findViewById(R.id.image);
            if (i==0){
                image.setText("半生");
                image.setTextSize(30);
            }else if (i==1){
                image.setText("半世");
                image.setTextSize(30);
            }else {
                image.setText("半清醒");
                image.setTextSize(30);
            }
        }

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(views.get(position));
                return views.get(position);
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(views.get(position));
            }
        };

        viewpage.setAdapter(pagerAdapter);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 3){
            startActivity(new Intent(this, FuShengMain.class));
            Shaper.Save(true,this);
            this.finish();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}